package com.bll.tsdoa.utils;

import com.bll.tsdoa.entity.Part;
import com.bll.tsdoa.entity.Raw;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author bll
 * @version 1.0
 * @date 2025/3/31 17:32
 */
@Component
public class CommonUtils {

    public Double getMaxRawLength(List<Raw> rawList) {
        if (rawList == null || rawList.isEmpty()) {
            throw new IllegalArgumentException("原料列表为空");
        }

        Double maxRawLength = rawList.stream()
                .map(Raw::getLength)
                .filter(Objects::nonNull)
                .filter(length -> length > 0)
                .max(Double::compare)
                .orElseThrow(() -> new IllegalArgumentException("原料无有效长度数据"));

        return maxRawLength;
    }

    public Double getMinPartLength(List<Part> partList) {
        if (partList == null || partList.isEmpty()) {
            throw new IllegalArgumentException("零件列表为空");
        }

        Double minPartLength = partList.stream()
                .map(Part::getLength)
                .filter(Objects::nonNull)
                .filter(length -> length > 0)
                .min(Double::compare)
                .orElseThrow(() -> new IllegalArgumentException("零件无有效长度数据"));

        return minPartLength;
    }

    public Double getMaxPartLength(List<Part> partList) {
        if (partList == null || partList.isEmpty()) {
            throw new IllegalArgumentException("零件列表为空");
        }

        Double maxPartLength = partList.stream()
                .map(Part::getLength)
                .filter(Objects::nonNull)
                .filter(length -> length > 0)
                .max(Double::compare)
                .orElseThrow(() -> new IllegalArgumentException("零件无有效长度数据"));

        return maxPartLength;
    }
}
